<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>

        <el-col :span="12">
          <el-form-item :label="$t('合同生效时间')" prop="contractEntryTime">
            <el-date-picker
              v-model="form.contractEntryTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('合同生效时间')"
              @change="checkDate"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('合同失效时间')" prop="contractTerminationTime">
            <el-date-picker
              v-model="form.contractTerminationTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('合同失效时间')"
              @change="checkDate"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('第几次签订')" prop="signNum">
            <el-input v-model="form.signNum" :placeholder="$t('第几次签订')" show-word-limit maxlength="20" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('续签时间')" prop="renewalTime">
            <el-date-picker
              v-model="form.renewalTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('续签时间')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="hr:contract" />
    </div>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { saveHrBaseContract, getHrBaseContract } from '@/api/oa/hr/hrBaseContract'

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        contractEntryTime: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        contractTerminationTime: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        signNum: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getHrBaseContract(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        hrId: undefined,
        contractEntryTime: undefined,
        contractTerminationTime: undefined,
        signNum: undefined,
        renewalTime: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid && this.checkDate()) {
            this.loading = true
            this.$set(this.form, 'hrId', this.$route.query.hrId)
            saveHrBaseContract(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.closeForm()
              } else {
                this.msgError(response.msg)
              }
              this.loading = false
            })
          } else {
            this.checkDate()
          }
        })
    },
    checkDate() {
        if (this.form.contractEntryTime === null || this.form.contractEntryTime === undefined || this.form.contractEntryTime === '' ||
            this.form.contractTerminationTime === null || this.form.contractTerminationTime === undefined || this.form.contractTerminationTime === '') {
          return true
        }
        const timeFlag = new Date(this.form.contractEntryTime).getTime() > new Date(this.form.contractTerminationTime).getTime()
        if (timeFlag) {
          this.$message({
            message: '合同生效时间不能大于合同失效时间',
            type: 'warning'
          })
        }
      return !timeFlag
    }

  }

}
</script>
